<%--
  Created by IntelliJ IDEA.
  User: Dhy
  Date: 2023/9/6
  Time: 14:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查看</title>
</head>
<body>
<div style="padding: 16px;">
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>


<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
    </div>
</script>

<script src="/layui/layui.js"></script>
<script>
    layui.use(['table', 'dropdown'], function(){
        var table = layui.table;
        var dropdown = layui.dropdown;

        // 创建渲染实例
        table.render({
            elem: '#test',//
            url: '${pageContext.request.contextPath}/teacher?method=teacherList', // 此处为静态模拟数据，实际使用时需换成真实接口
            page: false, //是否分页
            cols: [[
                // {field:'teacher_id', fixed: 'left', width:80, title: 'ID', sort: true},
                {field:'teacherId', width:134, title: 'id'},
                {field:'teacherName', width:134, title: '用户'},
                {field:'studentPassword', width:280, title: '密码'},
                {field:'teacherSex', width:280, title: '性别'},
                {field:'teacherAge', width:280, title: '年龄'},
                {field:'phone', width:134, title: '电话'},
                {field:'title', width:280, title: '职称'},
                {fixed: 'right',title: '操作',width: 134,minwidth:125,toolbar:'#barDemo'}
            ]],
            parseData: function(res){
                return{
                    "code":res.code,
                    "msg":res.msg,
                    "data":res.res
                };
            },
            error:function(res,msg){
                console.log(res,msg)
            }
        });

        // 触发单元格工具事件
        table.on('tool(test)', function(obj){ // 双击 toolDouble
            // debugger
            var data = obj.data; // 获得当前行数据
            // console.log(obj)
            dept = data;
            if(obj.event === 'edit'){
                layer.open({
                    title: '编辑',
                    type: 2,
                    area: ['80%','80%'],
                    content: '${pageContext.request.contextPath}/Teacher/edit.jsp',
                    end: function () {
                        table.reload('test');
                    }
                });
            } else {//删除
                debugger
                layer.confirm('确定删除吗?', {icon: 3, title:'提示'}, function(index){
                    // do something
                    $.ajax({
                        url: "${pageContext.request.contextPath}/teacher?method=delete&teacher_id="+obj.data.teacherId,   // ajax请求地址 地址/开头是接口，直接写是页面
                        type: 'get',  // 请求的方式，get\post
                        dataType: 'json', // 、json
                        success: function(result){  // 请求成功时的回调， result请求返回的数据。
                            if (result.code === 200){
                                //表格重新加载
                                table.reload('test');
                            }
                        }
                    });
                    layer.close(index);
                });

            }
        });

    });
</script>
</body>
</html>
